package com.jspgou.cms.action.admin.main;

import static com.jspgou.common.page.SimplePage.cpn;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.LogMng;
import com.jspgou.core.web.SiteUtils;
import com.jspgou.core.web.WebErrors;
import com.jspgou.cms.entity.ShopLevel;
import com.jspgou.cms.manager.ShopLevelMng;

@Controller
public class ShopLevelAct {
	private static final Logger log = LoggerFactory.getLogger(ShopLevelAct.class);

	@RequestMapping("/shopLevel/v_list.do")
	public String list(Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(cpn(pageNo), CookieUtils
				.getPageSize(request));
		model.addAttribute("pagination",pagination);
		model.addAttribute("pageNo",pageNo);
		return "shopLevel/list";
	}

	@RequestMapping("/shopLevel/v_add.do")
	public String add(ModelMap model) {
		return "shopLevel/add";
	}

	@RequestMapping("/shopLevel/v_edit.do")
	public String edit(Long id, Integer pageNo, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.addAttribute("shopLevel", manager.findById(id));
		model.addAttribute("pageNo", pageNo);
		return "shopLevel/edit";
	}

	@RequestMapping("/shopLevel/o_save.do")
	public String save(ShopLevel bean, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean);
		log.info("save ShopLevel id={}", bean.getId());
		logMng.operating(request, "添加店铺等级", "id="+bean.getId()+" ;name="+bean.getLevelname());
		return "redirect:v_list.do";
	}

	@RequestMapping("/shopLevel/o_update.do")
	public String update(ShopLevel bean, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		/*WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}*/
		bean = manager.update(bean);
		log.info("update ShopLevel id={}.", bean.getId());
		logMng.operating(request, "修改店铺等级", "id="+bean.getId()+" ;name="+bean.getLevelname());
		return list(pageNo, request, model);
	}
	
	@RequestMapping("/shopLevel/o_order.do")
	public String order(Long[] wids,Long root, Integer[] lvorder, Integer pageNo,
			HttpServletRequest request, ModelMap model,Long tid) {
		WebErrors errors = validateOrder(wids, lvorder, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		manager.updateOrder(wids, lvorder);
		model.addAttribute("message", "global.success");
	
			return "redirect:v_list.do";
		
	
	}

	@RequestMapping("/shopLevel/o_delete.do")
	public String delete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ShopLevel[] beans = manager.deleteByIds(ids);
		for (ShopLevel bean : beans) {
			log.info("delete ShopLevel id={}", bean.getId());
			logMng.operating(request, "删除店铺等级", "id="+bean.getId()+" ;name="+bean.getLevelname());
		}
		return list(pageNo, request, model);
	}

	private WebErrors validateSave(ShopLevel bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
	/*	bean.setWebsite(web);*/
		return errors;
	}
	
	private WebErrors validateEdit(Long id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		if (vldExist(id, web.getId(), errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Long id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		if (vldExist(id, web.getId(), errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateDelete(Long[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		errors.ifEmpty(ids, "ids");
		for (Long id : ids) {
			vldExist(id, web.getId(), errors);
		}
		return errors;
	}

	private boolean vldExist(Long id, Long webId, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		ShopLevel entity = manager.findById(id);
		if(errors.ifNotExist(entity, ShopLevel.class, id)) {
			return true;
		}
		return false;
	}
	
	
	
	private WebErrors validateOrder(Long[] wids, Integer[] priority,
			HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (errors.ifEmpty(wids, "wids")) {
			return errors;
		}
		if (errors.ifEmpty(priority, "priority")) {
			return errors;
		}
		
		if (wids.length != priority.length) {
			errors.addErrorString("wids length not equals priority length");
			return errors;
		}
		for (int i = 0, len = wids.length; i < len; i++) {
			vldExist(wids[i],null, errors);
			if (priority[i] == null) {
				priority[i] = 0;
			}
		}
		return errors;
	}
	
	
	
	@Autowired
	private ShopLevelMng manager;
	@Autowired
	private LogMng logMng;
	
}